/*
 * @author: sanbao
 * @Date: 2022-07-22 14:21:10
 */
import { createStore } from 'vuex'
import LoginModel from '@/api/login'
import {
  setToken,
  getToken,
  setUserList,
  getUserList,
} from '../utils/auth'
export default createStore({
  state: {
    token: getToken() ? getToken() : '',
    userlist: getUserList() ? getUserList() : ''
  },
  getters: {
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    setUserList(state, userlist) {
      state.userlist = userlist
      setUserList(userlist)
    }
  },
  actions: {
    // 登陆
    handellogin({ commit }, userlist) {
      return new Promise(async (resolve, reject) => {
        try {
          const response = await LoginModel.postuserslist(userlist)
          if (response.data.meta.status == 200) {
            const token = response.data.data.token
            const userlist = response.data.data
            // token
            commit("setToken", token)
            // 用户信息
            commit("setUserList", userlist)
          }
          resolve(response)
        } catch (err) {
          reject(err)
        }
      })
    }
  },
  modules: {
  }
})
